<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/25
 * Time: 14:42
 */
namespace app\shop\controller;
use think\Db;
class Recycle extends Base
{
    public function index(){
        extract(input());
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $map = [];
        $map['uy_user_id'] = session('user_id');
        $map['uy_status'] = 2;
        $data = Db::table('up_user_yun')
            ->where($map)
            ->order('uy_type asc,uy_time desc')
            ->paginate(10);
        $this->assign([
            'data' => $data,
            'pages'=>$pages
        ]);
        return $this->fetch ();
    }
    /**
     * 恢复还原
     *
     * @return \think\response\Json
     */
    public function dataBack()
    {
        extract(input());
        Db::startTrans();
        try {
            $ids = explode(',',trim($ids,','));
            $data = Db::table('up_user_yun')
                ->where('uy_id','in',$ids)
                ->select();
            foreach ($data as $k => $v){
                if($v['uy_fid'] == 0){
                    Db::table('up_user_yun')
                        ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                        ->setField('uy_status',1);
                }else{
                    //先不管上下级什么的，先该再说
                    $have = Db::table('up_user_yun')
                        ->where('uy_status','in','2,3')
                        ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                        ->find();
                    if($have){
                        Db::table('up_user_yun')
                            ->where('uy_status','in','2,3')
                            ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                            ->setField('uy_status',1);
                    }
                    //在考虑数据正确性的问题（防止被删掉）
                    $have2 = Db::table('up_user_yun')
                        ->where('uy_status',1)
                        ->where('uy_id',$v['uy_fid'])
                        ->find();
                    if(!$have2){
                        //先把这个的父id改为0，放到最上面去
                        Db::table('up_user_yun')->where('uy_id',$v['uy_id'])->setField('uy_fid',0);
                        //把包含自己的已经正常的数据拿出来，重新赋值str
                        $last = Db::table('up_user_yun')
                            ->where('uy_status',1)
                            ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                            ->select();
                        $idstr = $v['uy_id_str'];
                        $index = stripos($idstr,','.$v['uy_id'].',');
                        $no = substr($idstr,0,$index+1); //这是要去掉的一截
                        foreach ($last as $kkk => $vvv){
                            $newstr = str_replace($no,',',$vvv['uy_id_str']);
                            Db::table('up_user_yun')->where('uy_id',$vvv['uy_id'])->setField('uy_id_str',$newstr);
                        }
                    }
                }
            }
            Db::commit();
            return json(['code'=>200,'msg'=>'操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code'=>0,'msg'=>'操作失败，请刷新测试']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function delReal()
    {
        extract(input());
        Db::startTrans();
        try {
            $allsize = 0;
            if($ids == -1){
                $map = [];
                $map['uy_user_id'] = session('user_id');
                $map['uy_status'] = 2;
                $data = Db::table('up_user_yun')
                    ->where($map)
                    ->select();
            }else{
                $data = Db::table('up_user_yun')
                    ->where('uy_id','in',$ids)
                    ->select();
            }

            foreach ($data as $k => $v){
                $have = Db::table('up_user_yun')
                    ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                    ->find();
                if($have){
                    $size = Db::table('up_user_yun')
                        ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                        ->sum('uy_size');
                    $allsize += $size;
                    Db::table('up_user_yun')
                        ->where('uy_id_str','like','%'.$v['uy_id_str'].'%')
                        ->delete();
                }
            }
            if($allsize > 0){
                $user_id = session('user_id');
                Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_ram_use',$allsize);
            }
            Db::commit();
            return json(['code'=>200,'msg'=>'操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code'=>0,'msg'=>'操作失败，请刷新测试']);
        }
    }
}